package com.guang.syncronized.syncronizedwaitset;

import com.guang.syncronized.SyncronziedDemoEight;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Description
 * @Author liguang
 * @Date 2022/10/23/13:04
 */
public class SyncronziedWaitSetTestOne {
    private final static Logger logger = LoggerFactory.getLogger(SyncronziedWaitSetTestOne.class);
    static List<Thread> threadList = new ArrayList<>();
    static Object lock = new Object();
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(() -> {
                synchronized (lock) {
                    logger.info("thread execure");
                        // 这里无意义的睡眠，只是想让其在控制台上打印时好看一点
                    try {
                        TimeUnit.MICROSECONDS.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, "t" + i);
            threadList.add(thread);
        }
        logger.info("获取得到锁的顺序应该是正序0-9");

        synchronized (lock){
            for (Thread thread : threadList) {
                logger.info("{}-->启动顺序，正序0-9",thread.getName());
                thread.start();
                try {
                    // 为什么满足能够顺序调用start方法！也就是说，控制线程进入到syncronzied中的顺序
                    // 但是具体取决于是哪个线程被OS调用，取决于OS
                    TimeUnit.MICROSECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
